//
//  ELAppAPI.swift
//  WZApp
//
//  Created by evol on 2018/6/29.
//  Copyright © 2018年 evol. All rights reserved.
//

import UIKit
import Moya
import RxSwift
import RxCocoa

let provider = MoyaProvider<ELAppAPI>()

public enum ELAppAPI {
    case labels
    case annotationInfomation(id: NSInteger)
    case locationDatas(version: NSInteger)
    case nearestRoutePoint(point: CGPoint)
    case routeLines(startId: NSInteger, endId: NSInteger)
}

extension ELAppAPI: TargetType {
    public var baseURL: URL {
        return URL(string: "http://api.zjart.net.cn/v1/")!
    }
    
    public var path: String {
        switch self {
        case .labels:
            return "labels/"
        case .annotationInfomation(_):
            return "annotation_data/"
        case .locationDatas(_):
            return "pic_gis_point/"
        case .nearestRoutePoint(_):
            return "markpoint/nearest/"
        case .routeLines(_, _):
            return "shortestpath_angle/"
        }
    }
    
    public var method: Moya.Method {
        switch self {
        case .locationDatas(_):
            return .get
        case .nearestRoutePoint(_):
            return .get
        default:
            return .post
        }
    }
    
    public var sampleData: Data {
        return "{}".data(using: .utf8)!
    }
    
    func parameters() -> Dictionary<String, Any>? {
        switch self {
        case .labels:
            return ["site": "wuzhen"]
        case .annotationInfomation(let id):
            return ["site": "wuzhen", "id": id, "language": 1]
        case .locationDatas(let version):
            return ["site": "wuzhen", "version": version]
        case .nearestRoutePoint(let point):
            return ["gis_x": point.x, "gis_y": point.y, "site": "wuzhen"]
        case .routeLines(let startId, let endId):
            return ["source_id": startId, "destination_id": endId, "site": "wuzhen", "version": "2"]
        default:
            return nil
        }
    }
    
    public var task: Task {
        let parameters = self.parameters()
        if parameters != nil {
            return .requestParameters(parameters: parameters!, encoding:URLEncoding.default)
        }else {
            return .requestPlain
        }
    }
    
    public var headers: [String : String]? {
        return nil
    }
    
}
